package com.xinsoft.controller.basic;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Maps;
import com.xinsoft.aop.ano.SysLog;
import com.xinsoft.common.BusinessType;
import com.xinsoft.common.Response;
import com.xinsoft.common.Tree;
import com.xinsoft.constant.Const;
import com.xinsoft.entity.dto.SysDeptDto;
import com.xinsoft.entity.dto.SysDeptUserDto;
import com.xinsoft.entity.po.SysDept;
import com.xinsoft.entity.po.SysUser;
import com.xinsoft.entity.vo.SysDeptVo;
import com.xinsoft.service.SysDeptService;
import com.xinsoft.service.SysDictionaryService;
import com.xinsoft.service.SysUserService;
import com.xinsoft.utils.tree.DeptNodeParser;
import com.xinsoft.utils.tree.DeptUserNodeParser;
import com.xinsoft.validator.ValidatorUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @ClassName SysDeptController
 * @Description 部门管理控制层
 * @Author lijie
 * @Date 2023-5-17 09:51:32
 * @Version 1.0
 */
@RestController
@RequestMapping("/system/dept")
@Api(value = "SysDeptController", tags = {"部门管理控制层"})
public class SysDeptController {
    @Autowired
    private SysDeptService sysDeptService;

    @Autowired
    private SysDictionaryService dictionaryService;


    /**
     * @return com.xinsoft.common.Response<?>
     * @Author Li9527
     * @Description 部门管理-新增
     * @Date 2023/5/17 9:57
     * @params dept
     **/
    @SysLog(title = "部门管理-新增", businessType = BusinessType.INSERT)
    @ApiOperation("新增")
    @PostMapping("/add")
    public Response<?> add(@RequestBody SysDept dept) {
        // 参数校验
        ValidatorUtils.validateEntity(dept);
        return Response.succeed(sysDeptService.save(dept));
    }

    /**
     * @return com.xinsoft.common.Response<?>
     * @Author Li9527
     * @Description 更新
     * @Date 2023/5/17 9:57
     * @params dept
     **/
    @ApiOperation("更新")
    @SysLog(title = "部门管理-编辑", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public Response<?> edit(@RequestBody SysDept dept) {
        // 参数校验
        ValidatorUtils.validateEntity(dept);
        return Response.succeed(sysDeptService.updateById(dept));
    }

    @Autowired
    SysUserService userService;

    /**
     * @return com.xinsoft.common.Response<?>
     * @Author Li9527
     * @Description 获取详情
     * @Date 2023/5/17 9:57
     * @params id
     **/
    @ApiOperation("获取详情")
    @GetMapping("/detail")
    public Response<?> detail(@RequestParam(value = "id", defaultValue = "", required = true) Integer id) {
        // 获取本体, 注入本体
        SysDeptVo vo = new SysDeptVo();
        SysDept dept = sysDeptService.getById(id);
        BeanUtil.copyProperties(dept, vo);
        // 组装
        if (dept.getDeptLeader() != null) {
            vo.setDeptLeaderName(userService.getById(dept.getDeptLeader()).getRealName());
        }
        if (dept.getParentId() != null && dept.getParentId() != 0) {
            vo.setParentIdName(sysDeptService.getById(dept.getParentId()).getDeptName());
        }
        if (dept.getBranchedLeaders() != null) {
            vo.setBranchedLeadersName(userService.getById(dept.getBranchedLeaders()).getRealName());
        }
        if (dept.getDeptType() != null) {
            vo.setTypeName(dictionaryService.getById(dept.getDeptType()).getDicName());
        }
        return Response.succeed(vo);
    }

    /**
     * @return com.xinsoft.common.Response<?>
     * @Author Li9527
     * @Description 删除
     * @Date 2023/5/12 9:20
     * @params ids
     **/
    @ApiOperation("删除")
    @SysLog(title = "部门管理-删除", businessType = BusinessType.DELETE)
    @PostMapping("/del")
    public Response<?> del(@RequestBody List<Integer> ids) {
        sysDeptService.removeByIds(ids);
        return Response.succeed();
    }

    /**
     * @return com.xinsoft.common.Response<?>
     * @Author xuhb
     * @Description 部门树结构
     * @Date 2023/5/17 10:26
     * @params
     **/
    @ApiOperation("获取部门树结构")
    @GetMapping("/getDeptTree")
    public Response<?> getDeptTree() {
        Map<String, Object> results = Maps.newHashMap();
        Tree<SysDeptDto> tree = new Tree<>();
        tree = sysDeptService.getDeptTree();
        return Response.succeed(tree, "获取成功");
    }

    /**
     * @return com.xinsoft.common.Response<?>
     * @Author xuhb
     * @Description 获取所有部门不分页
     * @Date 2023/5/17 10:26
     * @params
     **/
    @ApiOperation("获取所有部门不分页")
    @GetMapping("/getDeptList")
    public Response<?> getDeptList() {
        List<SysDeptDto> depList = sysDeptService.getDeptList();
        return Response.succeed(depList, "获取成功");
    }

    /**
     * @return com.xinsoft.common.Response<?>
     * @Author xuhb
     * @Description 获取所有部门不分页
     * @Date 2023/5/17 10:26
     * @params
     **/
    @ApiOperation("获取该部门下所有人")
    @GetMapping("/getDeptUserList")
    public Response<?> getDeptUserList(@RequestParam(value = "deptId", required = true) Integer deptId) {
        List<SysUser> depList = userService.list(new QueryWrapper<SysUser>()
                .eq("dept_id",deptId)
                .eq("status",Const.USER_STATUS_NORMAL)
                .gt("id",0));
        return Response.succeed(depList);
    }


    /**
     * @return com.xinsoft.common.Response<?>
     * @Author xuhb
     * @Description 获取所有部门不分页
     * @Date 2023/5/17 10:26
     * @params
     **/
    @ApiOperation("根据部门查子部门")
    @GetMapping("/getDeptListByPid")
    public Response<?> getDeptListByPid(@RequestParam(value = "pid", required = true) Integer pid) {
        List<SysDeptDto> list = sysDeptService.getDeptList();
        return Response.succeed(TreeUtil.build(list, pid.toString(), new DeptNodeParser()));
    }

    /**
     * @return com.xinsoft.common.Response<?>
     * @Author xuhb
     * @Description 树取所有部门及部门下的人
     * @Date 2023/5/17 10:26
     * @params
     **/
    @ApiOperation("树取所有部门及部门下的人")
    @GetMapping("/test")
    public Response<?> test() {
        List<SysDeptUserDto> list = sysDeptService.getDeptUserTree();
        return Response.succeed(TreeUtil.build(list, Const.DEFAULT_PID, new DeptUserNodeParser()));
    }

}
